package com.rongke.web.controller.medical;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.rongke.commons.JsonResp;
import com.rongke.model.MedicalAdvice;
import com.rongke.model.MedicalRecords;
import com.rongke.service.MedicalAdviceService;
import com.rongke.service.MedicalRecordsService;
import com.rongke.utils.StringUtil;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 精准治疗建议管理接口
 */
@RestController
@RequestMapping(value = "/api/advice")
@Transactional
public class AdviceController {
	private static final Logger log = Logger.getLogger(AdviceController.class);

	@Autowired
	private MedicalAdviceService medicalAdviceService;
	@Autowired
	private MedicalRecordsService medicalRecordsService;

	/**
	 * 根据病历ID查询精准治疗建议，只在病历状态=3时才会输出
	 * 
	 * @param medicalRecords
	 * @return
	 */
	@RequestMapping(value = "/query", method = RequestMethod.GET)
	public JsonResp queryAdvice(long medicalRecordId) {
		log.debug("queryAdvice");

		try {
			MedicalRecords mr = medicalRecordsService
					.selectById(medicalRecordId);
			if (null == mr || mr.getStatus() != 3) {
				return JsonResp.fa(null);
			}

			Map<String, Object> param = new HashMap<String, Object>();
			param.put("medical_record_id", medicalRecordId);
			List<MedicalAdvice> medicalAdvices = medicalAdviceService
					.selectByMap(param);
			if (CollectionUtils.isEmpty(medicalAdvices)) {
				return JsonResp.fa(null);
			}

			MedicalAdvice medicalAdvice = medicalAdvices.get(0);
			if (null == medicalAdvice) {
				return JsonResp.fa(null);
			}

			JSONObject json = new JSONObject();
			json.put("medicalRecordId", medicalRecordId);
			json.put("verifiedCheckAdvice",
					medicalAdvice.getVerifiedCheckAdvice());
			json.put("verifiedTreateAdvice",
					medicalAdvice.getVerifiedTreateAdvice());
			return JsonResp.ok(json);
		} catch (Exception e) {
			log.error("queryByUserId failed", e);
			return JsonResp.fa(null);
		}
	}
}
